/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/

#include "pch.h"
#include <db/db_table_definition.h>
#include <assert.h>

namespace test_wrappers { namespace storage {

table_definition::table_definition(
    const std::wstring& name) :
    _name(name)
{
}

std::wstring table_definition::name() const
{
    return _name;
}

void table_definition::setName(
    const std::wstring& name
    )
{
    _name = name;
}

void table_definition::addIndex(
    const index_definition& newIndex)
{
    for (auto &index : _indices)
    {
        if (index.second.name() == newIndex.name())
        {
            throw db_exception("duplicate index name", e_db_duplilcate_index_name);
        }
    }
    _indices[newIndex.userIndexId()] = newIndex;
}

const index_definition& table_definition::index(
    const object_id& indexId) const
{
    if ( _indices.find(indexId) == _indices.end() )
    {
        throw db_exception("unknown index", e_db_unknown_index);
    }
    return _indices.at(indexId);
}

const tuple_definition& table_definition::tupleDefinition() const
{
    return _tupleDefinition;
}

std::vector<index_definition> table_definition::indices() const
{
    std::vector<index_definition> indices;
    for (auto &index : _indices)
    {
        indices.push_back(index.second);
    }
    return indices;
}


}} // test_wrappers::storage
